<?php
namespace app\article\model;
use app\common\lib\Paginationm;
use app\common\lib\ChinesePinyin;
use think\Db;
use think\Controller;

class ArticlesField extends Controller
{
    protected $beforeActionList = ['start'];
    public function start() {
        $this->itemType = 'article';
        $this->itemName = '内容';
        $this->item = 'Articles';
        $this->itemCategory = 'ArticlesCategory';
        $this->itemContent = 'ArticlesContent';
        $this->itemSearch = 'ArticlesSearch';
        $this->itemSearchRel = 'ArticlesSearchRel';
        $this->itemTagsRel = 'ArticlesTagsRel';
        $this->itemModelNameSpace = 'app\article\model\Articles';
        $this->itemCategoryModelNameSpace = 'app\article\model\ArticlesCategory';
        $this->itemTagsRelModelNameSpace = 'app\article\model\ArticlesTagsRel';
        
        $this->domain = config('domain');
    }
     
    public function getItemListByTag($tag,$listType)
    {
        $tag = json_decode($tag,true);
        $whereArray = [];
        if (!$tag) {
            return false;
        }
        foreach ($tag as $key => $val) {
            if (strpos($key, 'where') !== false) {
                if (strpos($val, '=') !== false) {
                    $tempVal = explode('=', $val);
                    $whereArray[trim($tempVal[0])] = trim($tempVal[1]);
                }
                if (strpos($val, 'like') !== false) {
                    $tempVal = explode('like', $val);
                    $whereArray[trim($tempVal[0])] = ['like',trim($tempVal[1])];
                }
                if (strpos($val, '>') !== false) {
                    $tempVal = explode('>', $val);
                    $whereArray[trim($tempVal[0])] = ['>',trim($tempVal[1])];
                }
                if (strpos($val, '<') !== false) {
                    $tempVal = explode('<', $val);
                    $whereArray[trim($tempVal[0])] = ['<',trim($tempVal[1])];
                }
            }
        }
        $tag['whereArray'] = $whereArray;
        return $this->getItemList($tag);
        
    }

    public function getItemList($paramData)
    {
        $modelName = $paramData['modelName'] ? $paramData['modelName'] : 'news';
        $itemId = $paramData['itemId'] ? $paramData['itemId'] : '';
        $orderBy = $paramData['orderBy'] ? $paramData['orderBy'] : 'id';
        $order = $paramData['order'] ? $paramData['order'] : 'sort';
        
       	$modelWhere['model_name']  = ['=',$modelName];
        $itemList = db('ArticlesField')->where($modelWhere)->order($orderBy,$order)->select();
        if ($itemList) {
           foreach ($itemList as $key => $value) {
           	$articleInfo = db('Articles')->where('id',$itemId)->find();
           	$itemList[$key]['value'] = $articleInfo[$value['field']];
           }
        } else {
            $itemList = [];
        }
        return $itemList;
    }

}